//
//  CJTabBarController.swift
//  SinaWeibo
//
//  Created by 程建 on 16/9/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CJTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //这里不能直接设置值,因为是只读属性
        //self.tabBar = CJMainTabBar()
        
        let cjTabBar = CJMainTabBar()
        
        //注意这里会有循环引用
        cjTabBar.composeClosure = { [weak self] in
            
           let composeView = CJComposeView()
            
            //显示composeView
           // let window = UIApplication.shared.keyWindow
           // window?.addSubview(composeView)
            
            composeView.show(target: self)
        }
        
        self.setValue(cjTabBar, forKey: "tabBar")
        
        // Do any additional setup after loading the view.
        addChildViewControllers()
    }

    //MARK:添加控制器
   private func addChildViewControllers() {
        //let home = CJHomeTableViewController()
       //首页
       /*
        home.navigationItem.title = "首页"
        home.tabBarItem.title = "首页"
        home.tabBarItem.image = #imageLiteral(resourceName: "tabbar_home")
        let nav = UINavigationController(rootViewController: home)
        addChildViewController(nav)
        */
        //首页
    addChildViewController(vc: CJHomeTableViewController(), title: "首页", imageName: "tabbar_home",index:0)
    
        //消息
    addChildViewController(vc: CJMessageTableViewController(), title: "消息", imageName: "tabbar_message_center",index:1)
    
        //发现
    addChildViewController(vc: CJDIscoverTableViewController(), title: "发现", imageName: "tabbar_discover",index:2)
    
        //我
    addChildViewController(vc: CJProdfileTableViewController(), title: "我", imageName: "tabbar_profile",index:3)
    
        
        
    }
    
    private func addChildViewController(vc:UIViewController, title: String, imageName:String,index:Int) {
        //设置title 首页
        vc.navigationItem.title = title
        //设置tabBar 同时需要设置图片才能显示
        vc.tabBarItem.title = title
        vc.tabBarItem.tag = index
        vc.tabBarItem.image = UIImage(named: imageName)
        
        
        /*字和图一起的时候
        vc.tabBarItem.image = #imageLiteral(resourceName: "allproducts_selected")
        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
        */
        
        //设置选中的图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: UIControlState.selected)
        
        //文字的偏移
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
        
        //设置文字的大小
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 12)], for: UIControlState.normal)
        
        //修改徽标的颜色 ,不显示
        vc.tabBarItem.badgeColor = UIColor.green
        vc.tabBarItem.badgeValue = nil
        
        
        let nav = CJNavigationViewController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    
    //当用户点击了UITabBarButton就会触发该事件
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

        //遍历子视图
        var index = 0
        for subview in tabBar.subviews {
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                //获取到UITabBarButton
             
                if index == item.tag {
                    //就已经查找到了被点击的UITabBarButton
                    //遍历tabbarButton的子视图 判断类型是否是xxximageView
                    for targer in subview.subviews {
                        if targer.isKind(of: NSClassFromString("UITabBarSwappableImageView")!) {
                            //做动画
                            //修改transform
                            targer.transform = CGAffineTransform.init(scaleX: 0.4, y: 0.4)
                            //在动画闭包中执行放大操作
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { 
                                targer.transform = CGAffineTransform.init(scaleX: 1, y: 1)
                                }, completion: { (_) in
                                    
                            })
                        }
                        
                    }
                    
                }
                index += 1
            }
        }
    }
    
}

